package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.constant.LifeConstant;
import com.csun.cmny.provider.dao.RoomMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcAppUserBuildingBind;
import com.csun.cmny.provider.model.domain.OmcLifeOldmanRoom;
import com.csun.cmny.provider.model.dto.life.LifeRoomIndexDto;
import com.csun.cmny.provider.model.dto.life.LifeRoomQueryDto;
import com.csun.cmny.provider.model.dto.oldmanApp.AppRoomDto;
import com.csun.cmny.provider.model.vo.LifeRoomBuildingVo;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.provider.model.vo.RoomInfoLifeVo;
import com.csun.cmny.provider.model.vo.RoomInfoVo;
import com.csun.cmny.provider.model.vo.life.LifeIndexRoomVO;
import com.csun.cmny.provider.model.vo.life.LifeRoomVO;
import com.csun.cmny.provider.pojo.Building;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.vo.RoomSleepInfo;
import com.csun.cmny.provider.vo.RoomWithOtherVo;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("RoomService")
@Transactional
public class RoomServiceImpl implements RoomService {
    @Resource
    RoomMapper roomMapper;
    @Resource
    private DmcRpcService dmcRpcService;
    @Resource
    private BuildingService buildingService;
    @Resource
    private OmcLifeOldmanRoomService omcLifeOldmanRoomService;
    @Resource
    private OmcAppUserBuildingBindService omcAppUserBuildingBindService;

    @Override
    public int insert(Room room) {
        return roomMapper.insertSelective(room);
    }

    @Override
    public int deleteById(Integer roomId) {
        return roomMapper.deleteByPrimaryKey(roomId);
    }

    @Override
    public int updateById(Room room) {
        return roomMapper.updateByPrimaryKeySelective(room);
    }

    @Override
    public Room selectById(Integer roomId) {
        return roomMapper.selectByPrimaryKey(roomId);
    }

    @Override
    public List<Room> showAll(Map<String, Object> queryMap) {
        return roomMapper.showAll(queryMap);
    }

    @Override
    public Map<String, Object> selectBuildingAndRoom(Integer roomId) {
        return roomMapper.selectBuildingAndRoom(roomId);
    }

    @Override
    public Room selectByRoom(Room room) {
        return roomMapper.selectByRoom(room);
    }

    @Override
    public List<Map<String, Object>> selectByBidAndFno(Integer buildingId, Integer floorNo) {
        return roomMapper.selectByBidAndFno(buildingId,floorNo);
    }

    @Override
    public List<Room> selectRooms(Room room) {
        return roomMapper.selectRooms(room);
    }

    @Override
    public List<RoomWithOtherVo> selectRoomsWithOther(Room room) {
        return roomMapper.selectRoomsWithOther(room);
    }

    @Override
    public Room selects(Integer roomId) {
        return roomMapper.selects(roomId);
    }

    /**
     * query room building list
     *
     * @param roomIdList room id list
     *
     * @return the list.
     */
    @Override
    public List<RoomBuildingVo> queryRoomBuildingList(List<Integer> roomIdList) {

        return roomMapper.queryRoomBuildingList(roomIdList);
    }

    /**
     * query room building by room id
     *
     * @param roomId room id
     * @return the room building vo
     */
    @Override
    public RoomBuildingVo queryRoomBuildingByRoomId(Integer roomId) {

        return roomMapper.queryRoomBuildingByRoomId(roomId);
    }

    /**
     * 校验房间别名是否存在
     *
     * @param roomCheck 房间条件
     * @return true-不存在 false-存在
     */
    @Override
    public boolean checkRoomAlias(Room roomCheck) {

        int result = roomMapper.selectCountRoom(roomCheck);
        log.info("result =====> {}", result);

        return result < 1;
    }

    @Override
    public List<RoomInfoVo> queryRoomInfo(Integer buildingId, Integer floorNo, Integer roomId, LoginAuthDto loginAuthDto) {

        List<RoomInfoVo> voList = roomMapper.queryRoomInfo(buildingId,floorNo,roomId);
        for(RoomInfoVo vo : voList){
            Integer checkInNum = roomMapper.queryRoomCheckInNum(vo.getRoomId());
            vo.setCheckInNum(checkInNum);
        }
        return voList;
    }

    @Override
    public List<RoomInfoLifeVo> queryRoomInfoLife() {
        List<RoomInfoLifeVo> voList = roomMapper.queryRoomLifeInfo();
        for(RoomInfoLifeVo vo : voList){

        }
        return voList;
    }

    @Override
    public Long selectOldByRoomId(Long roomId) {
        return roomMapper.selectOldByRoomId(roomId);
    }

    @Override
    public Integer saveLifeRoom(Room room, LoginAuthDto loginAuthDto) {

        //放入操作人信息和机构信息
        if(room.getRoomId() != null){
            room.setModifier(loginAuthDto.getUserId());
        }
        if(!checkLifeRoomName(room)){
            throw new OmcBizException(ErrorCodeEnum.OMC10070002);
        }
        if(room.getRoomId() == null){
            //房间添加
            int i = roomMapper.insertSelective(room);
            return i;
        }else{
            return roomMapper.updateByPrimaryKeySelective(room);
        }
    }

    @Override
    public List<LifeRoomVO> queryListWithPage(LifeRoomQueryDto lifeRoomQuertDto) {
        return roomMapper.queryListWithPage(lifeRoomQuertDto);
    }

    @Override
    public LifeRoomVO getById(Long roomId) {
        LifeRoomVO lifeRoom = roomMapper.getLifeRoomById(roomId.intValue());
        //查询房间绑定的摄像头
        List<String> cameraNo = dmcRpcService.getCameraDeviceNoByLifeRoomId(roomId);
        if(lifeRoom != null){
            lifeRoom.setCameraNo(cameraNo);
        }
        return lifeRoom;
    }

    @Override
    public Integer deleteLifeRoomById(Long lifeRoomId) {
        OmcLifeOldmanRoom lorCheck = new OmcLifeOldmanRoom();
        lorCheck.setRoomId(lifeRoomId);
        int i = omcLifeOldmanRoomService.selectCount(lorCheck);
        if(i > 0){
            throw new OmcBizException(ErrorCodeEnum.OMC10070001);
        }
        return roomMapper.deleteByPrimaryKey(lifeRoomId.intValue());
    }

    @Override
    public List<LifeIndexRoomVO> queryIndexRooms(LifeRoomIndexDto lifeRoomQuertDto) {
        return roomMapper.queryIndexRooms(lifeRoomQuertDto);
    }

    @Override
    public List<LifeIndexRoomVO> lifeRoom(Long roomId, Long groupId) {
        return roomMapper.lifeRoom(roomId,groupId);
    }



    private boolean checkLifeRoomName(Room lifeRoom) {
        Room roomCheck = new Room();
        roomCheck.setBuildingId(lifeRoom.getBuildingId());
        roomCheck.setRoomNo(lifeRoom.getRoomNo());
        List<Room> roomList = roomMapper.selectRooms(roomCheck);
        if(lifeRoom.getRoomId() != null && roomList.size() > 0){
            for (int i = roomList.size() - 1;i >= 0;i--) {
                if(roomList.get(i).getRoomId().equals(lifeRoom.getRoomId())){
                    roomList.remove(i);
                }
            }
        }
        int i = roomList.size();
        return i<1?true:false;
    }
    @Override
    public List<LifeRoomVO> notLifeRooms(LifeRoomQueryDto lifeRoomQuertDto){
        return roomMapper.notLifeRooms(lifeRoomQuertDto);
    }

    @Override
    public List<LifeRoomBuildingVo> listRoomByGroupId(Long groupId) {
        LifeRoomQueryDto dto = new LifeRoomQueryDto();
        dto.setGroupId(groupId);
        dto.setPageSize(1000);
        dto.setCheckInStatus(LifeConstant.CHECK_IN);
        return roomMapper.listRoomByGroupId(dto);
    }

    @Override
    public Integer userAddRoom(AppRoomDto appRoomDto, LoginAuthDto loginAuthDto) {

        OmcAppUserBuildingBind omcAppUserBuildingBind = omcAppUserBuildingBindService.selectByKey(loginAuthDto.getUserId());
        if(PublicUtil.isEmpty(omcAppUserBuildingBind)){
            omcAppUserBuildingBind = new OmcAppUserBuildingBind();
            omcAppUserBuildingBind.setUserId(loginAuthDto.getUserId());

            Building building = new Building();
            building.setBuildingName(loginAuthDto.getUserId() + "-个人app楼栋");
            building.setBuildingNo(loginAuthDto.getUserId() + "-个人app楼栋");
            building.setBuildingType(0);
            building.setHomeId(loginAuthDto.getGroupId().intValue());

            int insert = buildingService.insert(building);
            if(insert > 0){
                omcAppUserBuildingBind.setUserId(loginAuthDto.getUserId());
                omcAppUserBuildingBind.setBuildingId(building.getBuildingId().longValue());
                int i = omcAppUserBuildingBindService.save(omcAppUserBuildingBind);
            }
        }

        Room room = new Room();
        room.setBuildingId(omcAppUserBuildingBind.getBuildingId().intValue());
        room.setRoomNo(appRoomDto.getRoomNo());
        if(!checkLifeRoomName(room)){
            throw new OmcBizException(ErrorCodeEnum.OMC10070002);
        }
        return roomMapper.insertSelective(room);
    }

    /**
     * 守护佳个人床垫数据
     * @return Wrapper
     * @author HD
     * @date 2021/12/16 10:08
     */
    @Override
    public List<RoomSleepInfo> roomSleepInfo(Long groupId) {
        return roomMapper.roomSleepInfo(groupId);
    }

    @Override
    public List<RoomSleepInfo> queryRoomSleepInfo(ArrayList<Long> ids) {
        return roomMapper.queryRoomSleepInfo(ids);
    }
}
